package com.junzhiit.nengji.api.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.api.service.IncomeHistoryService;
import com.junzhiit.nengji.api.web.util.Constants;
import com.junzhiit.nengji.api.web.vo.ListVO;
import com.junzhiit.nengji.api.web.vo.ResultVO;
import com.junzhiit.nengji.api.web.vo.UserIncomeHistoryCountVO;
import com.junzhiit.nengji.api.web.vo.UserIncomeHistoryVO;
import com.junzhiit.nengji.bo.OrdersBO;
import com.junzhiit.nengji.bo.UserIncomeHistoryBO;
import com.junzhiit.nengji.bo.UserVipBO;
import com.junzhiit.nengji.dao.GoodsDataHistoryDAO;
import com.junzhiit.nengji.dao.IncomeHistoryDAO;
import com.junzhiit.nengji.dao.OrdersDataHistoryDAO;
import com.junzhiit.nengji.dao.RevenueSettingDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.UserConfigDAO;
import com.junzhiit.nengji.dao.UserDAO;
import com.junzhiit.nengji.dao.UserVipDAO;
import com.junzhiit.nengji.model.GoodsDataHistory;
import com.junzhiit.nengji.model.IncomeHistory;
import com.junzhiit.nengji.model.OrdersDataHistory;
import com.junzhiit.nengji.model.RevenueSetting;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.User;
import com.junzhiit.nengji.model.UserConfig;
import com.junzhiit.nengji.model.enums.Is;
import com.junzhiit.nengji.model.enums.OrdersDataHistoryStatus;
import com.junzhiit.nengji.model.enums.RevenueSettingType;

@Service
public class IncomeHistoryServiceImpl extends BaseServiceImpl implements IncomeHistoryService {

	@Autowired
	private UserDAO userDAO;
	
	@Autowired
	private RevenueSettingDAO revenueSettingDAO;
	
	@Autowired
	private IncomeHistoryDAO incomeHistoryDAO;
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private UserConfigDAO userConfigDAO;
	
	@Autowired
	private OrdersDataHistoryDAO ordersDataHistoryDAO;
	
	@Autowired
	private GoodsDataHistoryDAO goodsDataHistoryDAO;
	
	@Autowired
	private UserVipDAO userVipDAO;
	
	@Override
	public void addIncomeHistory(OrdersBO ordersBO,String parentId,String orderDataHistoryId,String goodsDataHistoryId) {
		User user = userDAO.query(ordersBO.getUserId());
		Double value;
		
		//商家推荐用户
		if(user.getIsShop() == Is.NO.toIntValue() && StringUtils.isNoneBlank(user.getParentId()) && !user.getParentId().equals(Constants.INDUSTRY_ROOT)){
			User parentUser = userDAO.query(user.getParentId());
			if(parentUser.getIsShop() == Is.YES.toIntValue()){
				IncomeHistory incomeHistory = new IncomeHistory();
				RevenueSetting revenueSetting = revenueSettingDAO.getRevenue(RevenueSettingType.S_U.toIntValue());
				value = (ordersBO.getPrice()*ordersBO.getGoodsNumber()) * revenueSetting.getValue()/100;
				incomeHistory.setCommissionRation(revenueSetting.getValue());
				incomeHistory.setCommissionMoney(value);
				incomeHistory.setIncomeType(revenueSetting.getType());
				incomeHistory.setUserId(ordersBO.getUserId());
				incomeHistory.setIncomeShopId(parentUser.getShopId());
				incomeHistory.setTransactionMoney(ordersBO.getPayMoney());
				incomeHistory.setOrderNumber(ordersBO.getOrderNumber());
				incomeHistory.setPayTime(ordersBO.getPayTime());
				incomeHistory.setTransactionType(ordersBO.getType());
				incomeHistory.setGoodsId(ordersBO.getGoodsId());
				incomeHistory.setOrderGoodsId(ordersBO.getOrderGoodsId());
				incomeHistory.setGoodsPrice(ordersBO.getPrice());
				incomeHistory.setGoodsNumber(ordersBO.getGoodsNumber());
				incomeHistory.setEventType(ordersBO.getIsOpenRebate());
				incomeHistoryDAO.insert(incomeHistory);
				
				//商家流水
				Shop shop = shopDAO.query(parentUser.getShopId());
				if(StringUtils.isNoneBlank(shop.getId())){
					shop.setWithdrawalMoney(shop.getWithdrawalMoney()+value);
					shop.setCommendMoney(shop.getCommendMoney() + value);
					shopDAO.update(shop);//更新余额
				}
				
				//更新资产明细及财务报表
				OrdersDataHistory ordersDataHistory = ordersDataHistoryDAO.query(orderDataHistoryId);
				if(ordersDataHistory != null){
					ordersDataHistory.setReferrerMoney(ordersDataHistory.getReferrerMoney()+value);
					ordersDataHistory.setStatus(OrdersDataHistoryStatus.COMPLETE.toIntValue());
					ordersDataHistoryDAO.update(ordersDataHistory);
				}
				
				GoodsDataHistory goodsDataHistory = goodsDataHistoryDAO.query(goodsDataHistoryId);
				if(goodsDataHistory != null){
					goodsDataHistory.setIsShare(Is.YES.toIntValue());
					goodsDataHistory.setShopGetBenefitRation(revenueSetting.getValue());
					goodsDataHistory.setShopGetBenefitMoney(value);
					goodsDataHistoryDAO.update(goodsDataHistory);
				}
			}
		}
		
		Shop shop = shopDAO.query(ordersBO.getShopId());
		if(StringUtils.isNoneBlank(shop.getUserId()) && !shop.getUserId().equals(Constants.INDUSTRY_ROOT)){
			IncomeHistory incomeHistory = new IncomeHistory();
			User parentUser = userDAO.query(shop.getUserId());
			if(parentUser.getIsShop() == Is.YES.toIntValue()){
				RevenueSetting revenueSetting = revenueSettingDAO.getRevenue(RevenueSettingType.S_S.toIntValue());
				value = (ordersBO.getPrice()*ordersBO.getGoodsNumber()) * revenueSetting.getValue()/100;
				incomeHistory.setCommissionRation(revenueSetting.getValue());
				incomeHistory.setCommissionMoney(value);
				incomeHistory.setIncomeType(revenueSetting.getType());
				incomeHistory.setShopId(ordersBO.getShopId());
				incomeHistory.setIncomeShopId(parentUser.getShopId());
				
				Shop parentShop = shopDAO.query(parentUser.getShopId());
				if(StringUtils.isNoneBlank(parentShop.getId())){
					parentShop.setWithdrawalMoney(parentShop.getWithdrawalMoney()+value);
					parentShop.setCommendMoney(parentShop.getCommendMoney() + value);
					shopDAO.update(parentShop);//更新余额
				}
				
				//更新资产明细及财务报表
				OrdersDataHistory ordersDataHistory = ordersDataHistoryDAO.query(orderDataHistoryId);
				if(ordersDataHistory != null){
					ordersDataHistory.setReferrerMoney(ordersDataHistory.getReferrerMoney()+value);
					ordersDataHistory.setStatus(OrdersDataHistoryStatus.COMPLETE.toIntValue());
					ordersDataHistoryDAO.update(ordersDataHistory);
				}
				
				GoodsDataHistory goodsDataHistory = goodsDataHistoryDAO.query(goodsDataHistoryId);
				if(goodsDataHistory != null){
					goodsDataHistory.setReferrerId(user.getParentId());
					goodsDataHistory.setReferrerName(parentUser.getNickName());
					goodsDataHistory.setReferrerIdentity(parentUser.getIsShop());
					goodsDataHistory.setReferrerRation(revenueSetting.getValue());
					goodsDataHistory.setReferrerMoney(value);
					goodsDataHistoryDAO.update(goodsDataHistory);
				}
			}else{
				RevenueSetting revenueSetting = revenueSettingDAO.getRevenue(RevenueSettingType.U_S.toIntValue());
				Double revenueValue = 0.0;
				UserVipBO userVipBO = userVipDAO.getVipByUserId(parentUser.getId());
				if(StringUtils.isNoneBlank(userVipBO.getId()) && userVipBO.getShopIncome() != null){
					revenueValue = userVipBO.getShopIncome();
				}else{
					revenueValue = revenueSetting.getValue();
				}
				value = (ordersBO.getPrice()*ordersBO.getGoodsNumber()) * revenueValue/100;
				incomeHistory.setCommissionRation(revenueSetting.getValue());
				incomeHistory.setCommissionMoney(value);
				incomeHistory.setIncomeType(revenueSetting.getType());
				incomeHistory.setShopId(ordersBO.getShopId());
				incomeHistory.setIncomeUserId(parentUser.getId());
				
				UserConfig userConfig = userConfigDAO.getUserConfig(parentUser.getId());
				if(StringUtils.isNoneBlank(userConfig.getId())){
					userConfig.setIncomeMoney(userConfig.getIncomeMoney() + value);
					userConfig.setPileIncomeMoney(userConfig.getPileIncomeMoney()+value);
					userConfig.setWithdrawalMoney(userConfig.getWithdrawalMoney()+value);
					userConfigDAO.update(userConfig);//更新余额
				}
				
				//更新资产明细及财务报表
				OrdersDataHistory ordersDataHistory = ordersDataHistoryDAO.query(orderDataHistoryId);
				if(ordersDataHistory != null){
					ordersDataHistory.setReferrerMoney(ordersDataHistory.getReferrerMoney()+value);
					ordersDataHistory.setStatus(OrdersDataHistoryStatus.COMPLETE.toIntValue());
					ordersDataHistoryDAO.update(ordersDataHistory);
				}
				
				GoodsDataHistory goodsDataHistory = goodsDataHistoryDAO.query(goodsDataHistoryId);
				if(goodsDataHistory != null){
					goodsDataHistory.setReferrerId(user.getParentId());
					goodsDataHistory.setReferrerName(parentUser.getNickName());
					goodsDataHistory.setReferrerIdentity(parentUser.getIsShop());
					goodsDataHistory.setReferrerRation(revenueSetting.getValue());
					goodsDataHistory.setReferrerMoney(value);
					goodsDataHistoryDAO.update(goodsDataHistory);
				}
				
			}
				incomeHistory.setTransactionMoney(ordersBO.getPayMoney());
				incomeHistory.setOrderNumber(ordersBO.getOrderNumber());
				incomeHistory.setPayTime(ordersBO.getPayTime());
				incomeHistory.setTransactionType(ordersBO.getType());
				incomeHistory.setGoodsId(ordersBO.getGoodsId());
				incomeHistory.setOrderGoodsId(ordersBO.getOrderGoodsId());
				incomeHistory.setGoodsPrice(ordersBO.getPrice());
				incomeHistory.setGoodsNumber(ordersBO.getGoodsNumber());
				incomeHistory.setEventType(ordersBO.getIsOpenRebate());
				incomeHistoryDAO.insert(incomeHistory);
		}
	}

	@Override
	public ResultVO getIncomeHistory(String userId,String date,Integer pageNumber, Integer maxPageRow) {
		ResultVO result = new ResultVO();
		if(StringUtils.isNotBlank(date)){
			date+=Constants.JOIN_DATE;
		}
		List<Shop> list = shopDAO.getRecommendByUserId(userId,pageNumber,maxPageRow);
		int count = shopDAO.getRecommendTotalCount(userId);
		UserConfig userConfig = userConfigDAO.getUserConfig(userId);
		UserIncomeHistoryCountVO userIncomeHistoryCountVO = new UserIncomeHistoryCountVO();
		if(list.size() > 0){
			for(Shop params : list){
				UserIncomeHistoryVO userIncomeHistoryVO = new UserIncomeHistoryVO();
				UserIncomeHistoryBO incomeHistoryBO = incomeHistoryDAO.totalUserIncomeHistory(userId, params.getId(), date);
				userIncomeHistoryVO.setShopId(params.getId());
				userIncomeHistoryVO.setIncomeMoney(incomeHistoryBO.getIncomeMoney());
				userIncomeHistoryVO.setShopIncome(incomeHistoryBO.getShopIncome());
				userIncomeHistoryVO.setShopName(params.getName());
				userIncomeHistoryCountVO.add(userIncomeHistoryVO);
			}
			userIncomeHistoryCountVO.setPileIncomeMoney(userConfig.getPileIncomeMoney());
			ListVO listVO = new ListVO();
	 		listVO.setIndex(pageNumber);
			listVO.setCount(count);
			listVO.setList(userIncomeHistoryCountVO);
			result.setData(listVO);
		}
		return result;
	}
}
